agentmux_srv\backend\wconfig/
watcher.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Thread-safe configuration holder with change notification.
5
6use std::sync::{Arc, RwLock};
7
8use super::types::{FullConfigType, SettingsType};
9
10/// Thread-safe configuration holder with change notification.
11/// The actual file system watching will be integrated with the
12/// event loop in a later phase.
13pub struct ConfigWatcher {
14    config: RwLock<Arc<FullConfigType>>,
15}
16
17impl ConfigWatcher {
18    /// Create a new config watcher with default config.
19    pub fn new() -> Self {
20        Self {
21            config: RwLock::new(Arc::new(FullConfigType::default())),
22        }
23    }
24
25    /// Create a new config watcher with initial config.
26    pub fn with_config(config: FullConfigType) -> Self {
27        Self {
28            config: RwLock::new(Arc::new(config)),
29        }
30    }
31
32    /// Get a snapshot of the current config.
33    pub fn get_full_config(&self) -> Arc<FullConfigType> {
34        self.config.read().unwrap().clone()
35    }
36
37    /// Get just the settings.
38    pub fn get_settings(&self) -> SettingsType {
39        self.config.read().unwrap().settings.clone()
40    }
41
42    /// Update the full config (called when files change).
43    #[allow(dead_code)]
44    pub fn set_config(&self, config: FullConfigType) {
45        let mut current = self.config.write().unwrap();
46        *current = Arc::new(config);
47    }
48
49    /// Update just the settings portion.
50    pub fn update_settings(&self, settings: SettingsType) {
51        let mut current = self.config.write().unwrap();
52        let mut new_config = (**current).clone();
53        new_config.settings = settings;
54        *current = Arc::new(new_config);
55    }
56}
57
58impl Default for ConfigWatcher {
59    fn default() -> Self {
60        Self::new()
61    }
62}